package b4.model.player
{
	import b4.model.basic.*;
	import b4.model.town.TownModel;
	import b4.model.config.*;
	import b4.net.*;
	
	public class Me extends Player
	{
		//对话号
		public var sessionId:String;
		//城镇信息 (id => TownModel)
		public var townList:Object;
		//主城 id
		public var mainTownId:int;
		
		public function Me()
		{
			super();
		}
		
		public function login(openId:String):void
		{
			this.openId = openId;
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.LOGIN, {openId:openId});
			serverRequest.addEventListener(ServerEvent.LOGIN, onLoginComplete);
			serverRequest.post(true);
		}
		
		private function onLoginComplete(event:ServerEvent):void
		{
			var jsonObj:Object = event.jsonObj;
			//注册自己信息
			this.loadbyObject(jsonObj.data.userInfo);
			var pm:PlayerManager = PlayerManager.getInstance(); 
			pm.registerMe(this);
			//更新城镇列表
			townList = new Object();
			for (var idStr:String in jsonObj.data.townList)
			{
				var townModel:TownModel = new TownModel();
				townModel.loadbyObject(jsonObj.data.townList[idStr]);
				townList[townModel.id] = townModel;
				//注册主城
				if (townModel.mainFlag)
					this.mainTownId = townModel.id;
			}
			//更新游戏配置
			GameConfig.basicConfig = jsonObj.data.basicConfig;
			var modelEvent:ModelEvent = new ModelEvent(ModelEvent.LOGIN_COMPLETE);
			modelEvent.jsonObj = jsonObj;
			dispatchEvent(modelEvent);
		}
	}
}